package com.kkd.kkdapp.utils;

import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;

public class KkdPlayerManager implements OnBufferingUpdateListener, OnCompletionListener,
		MediaPlayer.OnPreparedListener, SurfaceHolder.Callback,
		OnVideoSizeChangedListener, OnInfoListener, OnSeekCompleteListener,
		OnErrorListener {

	public MediaPlayer mediaPlayer;
	private SurfaceHolder surfaceHolder;
	private PlayerCallback mPlayerCallback;

	@SuppressWarnings("deprecation")
	public KkdPlayerManager(SurfaceView surfaceView, SeekBar skbProgress,
			PlayerCallback nPlayerCallback) {
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mPlayerCallback = nPlayerCallback;
	}

	public void playUrl(String videoUrl) {
		if (mediaPlayer != null) {
			try {
				mediaPlayer.reset();
				mediaPlayer.setDataSource(videoUrl);
				mediaPlayer.prepareAsync();// prepare之后自动播放
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public void pause() {
		if (mediaPlayer != null)
			mediaPlayer.pause();
	}

	private boolean isReady = false;

	public boolean isReady() {
		return isReady;
	}

	public void stop() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		System.out.println("surface changed");
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
		try {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setDisplay(surfaceHolder);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setOnBufferingUpdateListener(this);
			mediaPlayer.setOnPreparedListener(this);
			mediaPlayer.setOnCompletionListener(this);
			mediaPlayer.setOnErrorListener(this);
			mediaPlayer.setOnSeekCompleteListener(this);
			mediaPlayer.setOnInfoListener(this);
			mediaPlayer.setOnVideoSizeChangedListener(this);
		} catch (Exception e) {
			Log.e("mediaPlayer", "error", e);
		}
		System.out.println("surface created");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		System.out.println("surface destroyed");
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	@Override
	/**
	 * 通过onPrepared播放
	 */
	public void onPrepared(MediaPlayer arg0) {
		isReady = true;
		arg0.start();
		if (mPlayerCallback != null)
			mPlayerCallback.onPrepared(arg0);
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		if (mPlayerCallback != null)
			mPlayerCallback.onCompletion(arg0);
	}

	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
		if (mPlayerCallback != null)
			mPlayerCallback.onBufferingUpdate(arg0, bufferingProgress);
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		if (mPlayerCallback != null)
			mPlayerCallback.onError(mp, what, extra);
		return true;
	}

	@Override
	public void onSeekComplete(MediaPlayer mp) {
		if (mPlayerCallback != null)
			mPlayerCallback.onSeekComplete(mp);
	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		if (mPlayerCallback != null)
			mPlayerCallback.onInfo(mp, what, extra);
		return false;
	}

	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		if (mPlayerCallback != null)
			mPlayerCallback.onVideoSizeChanged(mp, width, height);
	}

	public boolean isPlaying() {
		if (mediaPlayer != null)
			return mediaPlayer.isPlaying();
		return false;
	}

	public interface PlayerCallback {
		public void onPrepared(MediaPlayer arg0);

		public void onCompletion(MediaPlayer arg0);

		public void onVideoSizeChanged(MediaPlayer mp, int width, int height);

		public boolean onInfo(MediaPlayer mp, int what, int extra);

		public void onSeekComplete(MediaPlayer mp);

		public boolean onError(MediaPlayer mp, int what, int extra);

		public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress);
	}

	public int getDuration() {
		if (mediaPlayer != null)
			return mediaPlayer.getDuration();
		return 0;
	}

	public void seekTo(int ms) {
		if (mediaPlayer != null)
			mediaPlayer.seekTo(ms);
	}

	public void setVolume(float mVolumn) {
		if (mediaPlayer != null)
			mediaPlayer.setVolume(mVolumn, mVolumn);
	}

	public void replay() {
		if (mediaPlayer != null){
//			mediaPlayer.reset();
			mediaPlayer.start();
		}
	}

	public int getCurrentPosition() {
		if (mediaPlayer != null)
			return mediaPlayer.getCurrentPosition();
		return 0;
	}

	public void reset() {
		if (mediaPlayer != null)
			mediaPlayer.reset();
	}

}
